package com.rc.activity;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rc.activity.dao.tbl_Report;
import com.rc.activity.dao.tbl_ReportDao;
import com.rc.activity.dao.tbl_UserDao;
import com.rc.activity.provider.Contract;
import com.rc.db.DatabaseSupport;
import com.rc.object.ReportObj;
import com.telpoo.frame.ui.BaseActivity;

public class ReportEdit extends BaseActivity implements OnClickListener {

	private LinearLayout llSave, llCancel, llReset;
	private TextView txtReportName, txtDate, txtSubmitter, txtApprover,
			txtReference;
	private tbl_Report objReport;

	public static void callIntent(Context ct, tbl_Report objReport) {
		Intent it = new Intent(ct, ReportEdit.class);
		Bundle b = new Bundle();
		b.putSerializable("OBJECT_REPORT", objReport);
		it.putExtras(b);
		ct.startActivity(it);
	}

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.report_create_new);
		((TextView) findViewById(R.id.lbTitle))
				.setText(getString(R.string.edit_report_info));
		llSave = (LinearLayout) findViewById(R.id.llSave);
		llCancel = (LinearLayout) findViewById(R.id.llCancel);
		llReset = (LinearLayout) findViewById(R.id.llReset);
		llSave.setOnClickListener(this);
		llCancel.setOnClickListener(this);
		txtReportName = (TextView) findViewById(R.id.txtReportName);
		txtDate = (TextView) findViewById(R.id.txtDate);
		txtSubmitter = (TextView) findViewById(R.id.txtSubmitter);
		txtApprover = (TextView) findViewById(R.id.txtApprover);
		txtReference = (TextView) findViewById(R.id.txtReference);

		llReset.setOnClickListener(this);
		llCancel.setOnClickListener(this);
		llSave.setOnClickListener(this);
		txtDate.setOnClickListener(this);

		// Function
		AddData();

	}

	/**
	 * get data reportdetailfragment
	 */
	private void AddData() {
		objReport = new ReportObj();
		if (getIntent() != null) {
			objReport = (tbl_Report) getIntent().getSerializableExtra(
					"OBJECT_REPORT");
			if (objReport == null) {
				return;
			}
		}
		Cursor cursor = this.getContentResolver().query(
				Contract.CONTENT_URI_USER, null, null, null, null);
		String mSubmitter = "";
		while (cursor.moveToNext()) {
			mSubmitter = cursor.getString(cursor
					.getColumnIndex(tbl_UserDao.Properties.Email.columnName));
		}

		txtReportName.setText(objReport.getTitle());
		txtReference.setText(objReport.getReference());
		txtReference.setEnabled(false);
		txtApprover.setText(objReport.getApproverEmail());
		txtSubmitter.setText(mSubmitter);
		txtDate.setText(objReport.getCreatedTime());
		showAndHiddenContent(DatabaseSupport.check_editReport(
				objReport.getReportID(), ReportEdit.this));
	}

	/**
	 * update data database local
	 */
	private int updateData() {
		int row = 0;
		objReport.setIsUpdate("1");
		objReport.setCreatedTime(txtDate.getText().toString());
		objReport.setTitle(txtReportName.getText().toString());
		objReport.setApproverEmail(txtApprover.getText().toString());
		row = this.getContentResolver().update(
				Contract.CONTENT_URI_REPORT,
				objReport.getContentValues(),
				tbl_ReportDao.Properties.ReportID.columnName + " = '"
						+ objReport.getReportID() + "'", null);
		return row;
	}

	/**
	 * check update is true
	 */

	private boolean checkUpdate() {
		if (objReport.getTitle().equals(txtReportName.getText().toString())
				&& objReport.getCreatedTime().equals(
						txtDate.getText().toString())
				&& objReport.getApproverEmail().equals(
						txtApprover.getText().toString())) {
			return true;
		}
		return false;
	}

	public void showDatePickerDialog() {
		int date = 0, month = 0, year = 0;

		OnDateSetListener callback = new OnDateSetListener() {
			public void onDateSet(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				// Date
				txtDate.setText((dayOfMonth) + "-" + (monthOfYear + 1) + "-"
						+ year);
			}
		};
		String s = txtDate.getText() + "";
		if (s.indexOf("-") >= 0) {
			String strArrtmp[] = s.split("-");
			date = Integer.parseInt(strArrtmp[0]);
			month = Integer.parseInt(strArrtmp[1]) - 1;
			year = Integer.parseInt(strArrtmp[2]);
			DatePickerDialog pic = new DatePickerDialog(ReportEdit.this,
					callback, year, month, date);
			pic.setTitle(getString(R.string.choose_calendar));
			pic.show();
		}

	}

	/**
	 * if The report is draft report, it is Show
	 */
	private void showAndHiddenContent(boolean isShow) {
		txtReportName.setEnabled(isShow);
		txtDate.setEnabled(isShow);
		txtReportName.setEnabled(isShow);
		txtApprover.setEnabled(isShow);
		if (isShow) {
			llSave.setVisibility(View.VISIBLE);
			llReset.setVisibility(View.VISIBLE);
		} else {
			llSave.setVisibility(View.GONE);
			llReset.setVisibility(View.GONE);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.llCancel:
			finish();
			break;
		case R.id.llSave:
			if (!checkUpdate()) {
				updateData();
			}
			finish();
			break;
		case R.id.llReset:
			AddData();
			break;
		case R.id.txtDate:
			showDatePickerDialog();
			break;
		default:
			break;
		}
	}

}
